import { Message } from "../models/message";

const message = new Message();
const app = getApp();
Component({
  data: {
    active: 0,
    list: [
      {
        url: "/pages/home/index",
        text: "智慧社区",
        active: "../static/icon/home-active.png",
        icon: "home-o",
        normal: "../static/icon/home.png",
      },
      {
        url: "/pages/message/index",
        text: "消息通知",
        active: "../static/icon/message-active.png",
        icon: "search",
        normal: "../static/icon/message.png",
      },
      {
        url: "/pages/my/index",
        text: "个人中心",
        active: "../static/icon/my-active.png",
        icon: "search",
        normal: "../static/icon/my.png",
      },
    ],
    isShow: false,
    timerId: null,
    s: false,
  },
  options: {
    styleIsolation: "shared",
  },
  methods: {
    onChange(event) {
      this.setData({ active: event.detail });
      wx.switchTab({
        url: this.data.list[event.detail].url,
      });
    },
    init() {
      const page = getCurrentPages().pop();
      this.setData({
        active: this.data.list.findIndex(
          (item) => item.url === `/${page.route}`
        ),
      });
      this.getReds();
      // if(!this.data.s){
      // 	this.setData({
      // 		s:true
      // 	},()=>{
      // 		app.watch(app, {
      // 			isShow:  (newVal)=> {
      // 				this.setData({
      // 					isShow:newVal
      // 				})
      // 			}
      // 		})
      // 	})
      // }
    },
    getReds() {
      let memberId = wx.getStorageSync("user").memberId;
      if (!memberId) {
        return;
      }

      message
        .getRed()
        .then((res) => {
          this.setData({
            isShow: res.result,
          });
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
});
